package cn.wangqi.personal.dao;

import cn.wangqi.personal.domain.User;
import cn.wangqi.personal.util.Page;
import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *created by admin  2017-09-07 11:24:12
 */
@Repository
public class UserDao{
    private final String NAMESPACE = this.getClass().getName() + ".";
    private Logger logger = Logger.getLogger(UserDao.class);
    @Resource
    protected SqlSessionTemplate mainSqlSession;

    public void saveUser(List<User> user) {
        mainSqlSession.insert(NAMESPACE + "saveUserList", user);
    }

    public void deleteUserById(int userId) {
        mainSqlSession.delete(NAMESPACE + "deleteUserById", userId);
    }

    public User findUserById(int userId){
        return mainSqlSession.selectOne(NAMESPACE + "findUserById", userId);
    }

    public void updateUser(User user){
        mainSqlSession.update(NAMESPACE + "updateUser", user);
    }

    public List<User> findUserList(Page page, User user){
        Map<String, Object> paramMap = new HashMap();
        paramMap.put("page", page);
        paramMap.put("user", user);
        return mainSqlSession.selectList(NAMESPACE + "findUserList", paramMap);
    }

    public int countUserList(User user){
        Map<String, Object> paramMap = new HashMap();
        paramMap.put("user", user);
        return mainSqlSession.selectOne(NAMESPACE + "countUserList",paramMap);
    }

    public User findUserByPhone(String phone){
        Map<String, Object> paramMap = new HashMap();
        paramMap.put("phone", phone);
        return mainSqlSession.selectOne(NAMESPACE + "findUserByPhone", paramMap);
    }

    public void saveUser(User user) {
        mainSqlSession.insert(NAMESPACE + "saveUser", user);
    }
}
